﻿using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Mars.GpuInstance
{
    public static class SerializeTool
    {
        [MenuItem("Assets/Instancer/序列化Instance文件")]
        public static void SerializeInstanceFile()
        {
            var selectingObjs = Selection.GetFiltered<UnityEngine.Object>(SelectionMode.DeepAssets);
            foreach (var obj in selectingObjs)
            {
                if (obj is GameObject)
                {
                    var selectObj = obj as GameObject;
                    if (selectObj)
                    {
                        var instance = selectObj.GetComponent<InstancerPrefab>();
                        if (instance != null)
                        {
                            SerializeInstanceFile( instance);
                        }
                    }
                }
            }
            
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        
        public static void SerializeInstanceFile(InstancerPrefab prefab)
        {
            if (prefab != null)
            {
               
                var prefabPath = InstancerEditorUtil.GetPrefabAssetPath(prefab.gameObject);
                var filePath = prefabPath.Replace(".prefab", "_cfg.asset");

                bool newCreate = false;
                var genFile = AssetDatabase.LoadAssetAtPath<InstanceGenConfig>(filePath);
                if (genFile == null)
                {
                    newCreate = true;
                    genFile = ScriptableObject.CreateInstance<InstanceGenConfig>();
                }

                var bound = prefab.GetMeshBounds();
                var boxCenter = bound.center;
                var boxExtents = bound.extents * prefab.boxExtents;

                genFile.boxCenter = boxCenter;
                genFile.boxExtents = boxExtents;
                genFile.forceRender = prefab.forceRender;
                genFile.shadowLod = prefab.shadowLod;
                
                genFile.lodConfigs.Clear();

                for (int i = 0; i < prefab.LodDatas.Count; i++)
                {
                    var instanceLodConfig = new InstanceLodConfig();
                    var lodData = prefab.LodDatas[i];
                    for (int j = 0; j < lodData.renderDatas.Count; j++)
                    {
                        var renderData = lodData.renderDatas[j];
                        var instanceRenderConfig = new InstanceRenderConfig();
                        instanceRenderConfig.mesh = renderData.mesh;
                        foreach (var mat in renderData.mats)
                        {
                            if (!mat)
                            {
                                continue;
                            }
                            instanceRenderConfig.materias.Add(mat);
                        }

                        instanceRenderConfig.transformOffset = renderData.transformOffset;
                        instanceLodConfig.renderConfigs.Add(instanceRenderConfig);
                    }

                    instanceLodConfig.showRange = lodData.lodDis;
                    genFile.lodConfigs.Add(instanceLodConfig);
                }
                
                if(genFile)
                {
                    if (newCreate)
                    {
                        AssetDatabase.CreateAsset(genFile, filePath);
                    }
                    EditorUtility.SetDirty(genFile);
                }
            }
        }

        public static string GetFolderByPath(string path)
        {
            var str = path.Substring(0, path.LastIndexOf('/'));
            return str;
        }
    }
}